package cn.m3a1.interceptor;

import cn.m3a1.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wyj
 * @date 2019/6/29
 * @time 下午4:45
 */
public class LoginInterceptor  implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        if (httpServletRequest.getRequestURL().indexOf("/login")>0){
            return true;
        }
        //判断是否含有session
        if (httpServletRequest.getSession().getAttribute("loginUser")!=null){
            User loginUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
            int type = loginUser.getType();
            //判断身份权限
            if (type==99&&httpServletRequest.getRequestURL().indexOf("/admin")>0){
                return true;
            }else if (type==70&&httpServletRequest.getRequestURL().indexOf("/head")>0){
                return true;
            }else if (type==50&&httpServletRequest.getRequestURL().indexOf("/teaCouse")>0){
                return true;
            }else if (type==1&&httpServletRequest.getRequestURL().indexOf("/student")>0){
                return true;
            }

        }
        httpServletRequest.getRequestDispatcher("/").forward(httpServletRequest,httpServletResponse);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
